//
//  AY_BookDCell.m
//  AiYue
//
//  Created by boWen on 2023/11/21.
//

#import "AY_BookDCell.h"
#import "AY_ShuDouItemCell.h"
#import "UIImage+GIF.h"

@interface AY_BookDCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UIView * bg_View;//底部圆角
@property (nonatomic,strong) UILabel * selectLabel;
@property (nonatomic, strong) UICollectionView * myCollectionView;
@property (nonatomic,strong) UIView * typeView;
@property (nonatomic,strong) UILabel * typeInfo1;
@property (nonatomic,strong) UIImageView * zfbImg;
@property (nonatomic,strong) UILabel * typeInfo2;
@property (nonatomic,strong) UIImageView * arrow;
@property (nonatomic,strong) UIView *lineView;
@property (nonatomic,strong) UIImageView * loadingView;
@end


@implementation AY_BookDCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}


- (void)addAllSubviewsWithUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(8) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];

    [self.bg_View addSubview:self.myCollectionView];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bg_View).offset(YNUM(-90));
        make.top.equalTo(self.bg_View);
        make.left.equalTo(self.bg_View).offset(YNUM(15));
        make.right.equalTo(self.bg_View).offset(YNUM(-15));
    }];
    
    self.surePyBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:[NSString stringWithFormat:@"立即%@",[AYMMKV getStringForKey:ZF]] andFont:[UIFont boldSystemFontOfSize:16] andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(handleSurePyAction) andRadius:YNUM(8) andAddSubView:self.bg_View];
    [self.surePyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myCollectionView.mas_bottom);
        make.left.equalTo(self.bg_View).offset(YNUM(16));
        make.right.equalTo(self.bg_View).offset(YNUM(-16));
        make.height.mas_offset(YNUM(50));
    }];
    
    [self.surePyBtn setBackgroundImage:[AYTool buttonImageFromColor:COLOR(@"dc5919")] forState:UIControlStateHighlighted];
        
    [self layoutIfNeeded];
    self.lineView = [AYTool createImageViewWithFrame:CGRectMake(-70, 0, 60, self.surePyBtn.mj_h) andImage:YImage(@"AY_detail_line") andBg:nil andRadius:0 andAddSubView:self.surePyBtn];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setTimer) userInfo:nil repeats:YES];

    self.typeView = [AYTool createViewWithFrame:CGRectZero andBg:nil andRadius:0 andAddSubView:self.bg_View];
    [self.typeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.bg_View);
        make.height.mas_equalTo(YNUM(35));
        make.top.equalTo(self.surePyBtn.mas_bottom).offset(YNUM(5));
    }];
    
    self.loadingBack = [AYTool createViewWithFrame:CGRectZero andBg:COLOR(@"#FF9760") andRadius:YNUM(8) andAddSubView:self.bg_View];
    self.loadingBack.hidden = YES;
    [self.loadingBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myCollectionView.mas_bottom);
        make.left.equalTo(self.bg_View).offset(YNUM(16));
        make.right.equalTo(self.bg_View).offset(YNUM(-16));
        make.height.mas_offset(YNUM(50));
    }];
        
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertWithPyyAction)];
    [self.typeView addGestureRecognizer:tap];
    
    self.typeInfo2 = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#909090") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.typeView];
    [self.typeInfo2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeView);
        make.centerX.equalTo(self.typeView).offset(YNUM(20));
    }];
    
    self.arrow = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_mvp_arrow") andBg:nil andRadius:0 andAddSubView:self.typeView];
    [self.arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeInfo2.mas_right);
        make.centerY.equalTo(self.typeView);
        make.width.height.mas_equalTo(YNUM(14));
    }];
    
    self.zfbImg = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_ali_chase") andBg:nil andRadius:0 andAddSubView:self.typeView];
    [self.zfbImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeView);
        make.right.equalTo(self.typeInfo2.mas_left);
        make.width.height.mas_equalTo(YNUM(14));
    }];
    
    self.typeInfo1 = [AYTool createLabelWithFrame:CGRectZero andText:@"使用 " andTextColor:COLOR(@"#909090") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self.typeView];
    [self.typeInfo1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.typeView);
        make.right.equalTo(self.zfbImg.mas_left);
    }];
}

- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((SCREEN_W - YNUM(60))/3, YNUM(130));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = YNUM(15);
        layout.minimumInteritemSpacing = 0;
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[AY_ShuDouItemCell class] forCellWithReuseIdentifier:@"AY_ShuDouItemCell"];
        _myCollectionView = collectionView;
    }
    return _myCollectionView;
}

#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.vpDataAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_ShuDouItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_ShuDouItemCell" forIndexPath:indexPath];
    if (self.vpDataAry.count > indexPath.row) {
        cell.vpSubModel = self.vpDataAry[indexPath.row];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    VpProductModel * proModel = self.vpDataAry[indexPath.row];
    [self.surePyBtn setTitle:[NSString stringWithFormat:@"立即%@%@元", [AYMMKV getStringForKey:ZF], proModel.price] forState:UIControlStateNormal];
    if ([proModel.tipStr isEqual:@"0"] || [proModel.tipStr isEqual:@"10"]) {
        self.typeInfo2.text = [NSString stringWithFormat:@" %@%@，极速到账",[AYMMKV getStringForKey:ZFB], [AYMMKV getStringForKey:ZF]];
    }else {
        self.typeInfo2.text = [NSString stringWithFormat:@" %@%@，限时%@折",[AYMMKV getStringForKey:ZFB],[AYMMKV getStringForKey:ZF],proModel.tipStr];
    }
    if(self.bookDBlock) {
        self.bookDBlock(indexPath.row);
    }
}

- (void)setTimer {
    [UIView animateWithDuration:2 animations:^{
        self.lineView.frame = CGRectMake(self.surePyBtn.mj_w, 0, 60, self.surePyBtn.mj_h);
    } completion:^(BOOL finished) {
        self.lineView.frame = CGRectMake(-70, 0, 60, self.surePyBtn.mj_h);
    }];
}

#pragma mark - 确认
- (void)alertWithPyyAction {
    if(self.dPyBlock) {
        self.dPyBlock();
    }
}

- (void)handleSurePyAction {
    if ([self.delegate respondsToSelector:@selector(ktAction)]) {
        [self.delegate ktAction];
    }
}

- (void)sendVpDataAry:(NSArray *)vpDataAry andPType:(NSString *)pType {
    _vpDataAry = vpDataAry;
    NSString * path = [[NSBundle mainBundle] pathForResource:@"AY_mvp_loading1" ofType:@"gif"];
    NSData * data = [NSData dataWithContentsOfFile:path];
    UIImage *image = [UIImage sd_imageWithGIFData:data];
    
    [self.loadingView removeFromSuperview];
    self.loadingView = [AYTool createImageViewWithFrame:CGRectZero andImage:image andBg:nil andRadius:0 andAddSubView:self.loadingBack];
    [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.surePyBtn);
        make.width.mas_equalTo(YNUM(40));
        make.height.mas_equalTo(YNUM(10));
    }];
    
    if(_vpDataAry.count > 0) {
        [self.myCollectionView reloadData];
        [self.myCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
        VpProductModel * proModel = self.vpDataAry[0];
        [self.surePyBtn setTitle:[NSString stringWithFormat:@"立即%@%@元", [AYMMKV getStringForKey:ZF], proModel.price] forState:UIControlStateNormal];
        if ([pType isEqual:@"ZFB"]) {
            if ([proModel.tipStr isEqual:@"0"] || [proModel.tipStr isEqual:@"10"]) {
                self.typeInfo2.text = [NSString stringWithFormat:@" %@%@，极速到账",[AYMMKV getStringForKey:ZFB], [AYMMKV getStringForKey:ZF]];
            }else {
                self.typeInfo2.text = [NSString stringWithFormat:@" 限时%@折",proModel.tipStr];
            }
            self.zfbImg.image = YImage(@"icon_ali_chase");
        }else {
            self.typeInfo2.text = [NSString stringWithFormat:@" %@%@，暂无优惠",[AYMMKV getStringForKey:WXSTR],[AYMMKV getStringForKey:ZF]];
            self.zfbImg.image = YImage(@"icon_weC_chase");
        }
        if(self.bookDBlock) {
            self.bookDBlock(0);
        }
    }
}
@end
